<?php
/**
 * @author Laurent SITTLER <lolo.sittler@gmail.com>
 * @author Alexandre GOMEZ <gomez.alexandre02@gmail.com>
 * @version 0.1
 */
require_once 'Library/Utils.php';

/**
 *  View database informations
 */
?>
<h3>Informations relatives à la base de données</h3>

<?php
if (Session::Get(Constants::$SESSION_cdb) == NULL) {
    echo '<div class = "ui-widget">';
    echo '<div class = "ui-state-error ui-corner-all" style = "margin-top: 20px; padding: 0 .7em;">';
    echo '<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>';
    echo 'Veuillez séléctionner une base de données !</p>';
    echo '</div>';
    echo '</div>';
} else {
    $cdb = new Database_Model();
    $infos = new Informations_Model();
    $error = Session::Get(Constants::$SESSION_error);
    if ($error == NULL) {
        $database = $cdb->GetById(Session::Get(Constants::$SESSION_cdb));
//    echo $database->GetRule()->GetName();

        echo '<div id="accordion">';
        // ADMIN
        if ($database->GetRule()->GetName() == 'Administrateur') {
            echo '<h3>Dictionnaire de données</h3>';
            echo '<div>';
            $resultDD1 = $infos->GetDictionaryData1();
            if (!empty($resultDD1)) {
//        print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultDD1[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $dd1 = 0;
                foreach ($resultDD1 as $tb) {
                    if ($dd1 % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $dd1++;
                }
                echo '</table><br />';
            }

            $resultDD2 = $infos->GetDictionaryData2();
            if (!empty($resultDD2)) {
//        print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultDD2[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $dd2 = 0;
                foreach ($resultDD2 as $tb) {
                    if ($dd2 % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $dd2++;
                }
                echo '</table><br />';
            }

            // String chart
            $nameStats = '';
            $valueStats = '';
            $totalStats = '';
            $resultDD3 = $infos->GetDictionaryData3();
            if (!empty($resultDD3)) {
//        print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultDD3[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $dd3 = 0;
                $size = 0;
                $free = 0;
                $use = 0;
                foreach ($resultDD3 as $tb) {
                    if ($dd3 % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $key => $value) {
                        if ($key == 'TABLESPACE')
                            $nameStats = $value;
                        if ($key == 'TAILLE TOTAL') {
                            $valueStats = $value;
                            echo '<td>' . Utils::bytesToSize($value) . '</td>';
                            $size += $value;
                        } else if ($key == 'LIBRE') {
                            echo '<td>' . Utils::bytesToSize($value) . '</td>';
                            $free += $value;
                        } else if ($key == 'UTILISE') {
                            echo '<td>' . Utils::bytesToSize($value) . '</td>';
                            $use += $value;
                        } else if ($key == '% D\'UTILISATION') {
                            if ($value < 1)
                                $cssTop = 'top: 0px;';
                            else
                                $cssTop = '';
                            echo '<td><div class="pourcent"><span class="pourcentResult" style="width:' . number_format($value) . '%;">&nbsp;</span><span class="pValue" style="' . $cssTop . '">' . number_format($value, 2, ',', ' ') . '%</span></div></td>';
                        }else {
                            echo '<td>' . $value . '</td>';
                        }
                    }
                    echo '</tr>';
                    $totalStats = $totalStats . '[\'' . $nameStats . '\', ' . $valueStats . '],';
                    $dd3++;
                }
                echo '<tr class="total"><td>Total de l\'état</td><td>' . Utils::bytesToSize($size) . '</td><td>' . Utils::bytesToSize($free) . '</td><td>' . Utils::bytesToSize($use) . '</td><td></td></tr>';
                echo '</table><br />';
            }
            $resultDD4 = $infos->GetDictionaryData4();
            if (!empty($resultDD4)) {
//        print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultDD4[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $dd4 = 0;
                $size = 0;
                $free = 0;
                $use = 0;
                foreach ($resultDD4 as $tb) {
                    if ($dd4 % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $key => $value) {
                        if ($key == 'TABLESPACE')
                            $nameStats = $value;
                        if ($key == 'TAILLE TOTAL') {
                            $valueStats = $value;
                            echo '<td>' . Utils::bytesToSize($value) . '</td>';
                            $size += $value;
                        } else if ($key == 'LIBRE') {
                            echo '<td>' . Utils::bytesToSize($value) . '</td>';
                            $free += $value;
                        } else if ($key == 'UTILISE') {
                            echo '<td>' . Utils::bytesToSize($value) . '</td>';
                            $use += $value;
                        } else if ($key == '% D\'UTILISATION') {
                            if ($value < 1)
                                $cssTop = 'top: 0px;';
                            else
                                $cssTop = '';
                            echo '<td><div class="pourcent"><span class="pourcentResult" style="width:' . number_format($value) . '%;">&nbsp;</span><span class="pValue" style="' . $cssTop . '">' . number_format($value, 2, ',', ' ') . '%</span></div></td>';
                        }else {
                            echo '<td>' . $value . '</td>';
                        }
                    }
                    echo '</tr>';
                    $totalStats = $totalStats . '[\'' . $nameStats . '\', ' . $valueStats . '],';
                    $dd4++;
                }
                echo '<tr class="total"><td>Total de l\'état</td><td>' . Utils::bytesToSize($size) . '</td><td>' . Utils::bytesToSize($free) . '</td><td>' . Utils::bytesToSize($use) . '</td><td></td></tr>';
                echo '</table><br />';
            }
            $totalStats = substr_replace($totalStats, "", -1);
            ?>
            <script type="text/javascript">
                google.load('visualization', '1.0', {'packages':['corechart']});
                google.setOnLoadCallback(drawChart);
                function drawChart() {

                    var data = new google.visualization.DataTable();
                    data.addColumn('string', 'Topping');
                    data.addColumn('number', 'Slices');
            <?php
            echo 'data.addRows([ ' . $totalStats . ' ]);';
            ?>
                    var options = {'title':'Taille des TABLESPACES',
                        'width':400,
                        'height':250};
                    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
                    chart.draw(data, options);
                }
            </script>
            <?php
            echo '<div id="chart_div" class="pieChart"></div>';
            echo '</div>';
            echo '<h3>Mémoire</h3>';
            echo '<div>';
            $resultMEM = $infos->GetMemory();
            if (!empty($resultMEM)) {
//        print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultMEM[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $mem = 0;
                $total = 0;
                foreach ($resultMEM as $row) {
                    if ($mem % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($row as $key => $value) {
                        if ($key == "BYTES") {
                            echo '<td>' . Utils::bytesToSize($value) . '</td>';
                            $total += $value;
                        } else {
                            echo '<td>' . $value . '</td>';
                        }
                    }
                    echo '</tr>';
                    $mem++;
                }
                echo '<tr class="total"><td>Total</td><td>' . Utils::bytesToSize($total) . '</td><td></td></tr>';
                echo '</table>';
            }
            echo '</div>';
            echo '<h3>Paramètres</h3>';
            echo '<div>';
            $resultParam = $infos->GetParameters();
            //print_r($result);
            if (!empty($resultParam)) {
                //print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultParam[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $param = 0;
                foreach ($resultParam as $tb) {
                    if ($param % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $param++;
                }
                echo '</table>';
            }
            echo '</div>';
            echo '<h3>Bases de données</h3>';
            echo '<div>';
            $resultDB = $infos->GetDatabases();
            //print_r($result);
            if (!empty($resultDB)) {
                //print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultDB[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineDb = 0;
                foreach ($resultDB as $tb) {
                    if ($lineDb % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineDb++;
                }
                echo '</table>';
            }
            echo '</div>';
            echo '<h3>Utilisateurs</h3>';
            echo '<div>';
            $resultUser = $infos->GetUsers();
            if (!empty($resultUser)) {
//    print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultUser[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineUser = 0;
                foreach ($resultUser as $tb) {
                    if ($lineUser % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineUser++;
                }
                echo '</table>';
                echo '<br/><br/>';
            }
            $resultOUser = $infos->GetUsersObjects();
            if (!empty($resultOUser)) {
//    print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultOUser[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineOUser = 0;
                $total = 0;
                foreach ($resultOUser as $tb) {
                    if ($lineOUser % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $key => $value) {
                        if ($key == 'TOTAL')
                            $total += $value;
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineOUser++;
                }
                echo '<tr class="total"><td>Total objets</td><td></td><td>' . $total . '</td></tr>';
                echo '</table>';
            }
            echo '</div>';
            echo '<h3>Script</h3>';
            echo "<div><p><pre>
					
_____________________________________SCRIPT DE SAUVEGARDE ORACLE________________________________________
					
md c:\ora_cold_backup
echo. > c:\ora_cold_backup\backup.sql
md c:\ora_save
sqlplus -s \"sys/mdp@NomTNS as sysdba\"

-- Variables d'environnement de SQL*PLUS de formatage de l'affichage
SET feedback off
SET pagesize 0
SET linesize 500
SET heading off
SET trimspool off
SET verify off

-- Penser à vérifier que les répertoires ciblés existent déjà
define folder='c:\ora_cold_backup'
define fichier_control='c:\ora_cold_backup\backup.sql'

spool &fichier_control
select 'host copy ' || name || ' &folder from v\$datafile order by 1;
select 'host copy ' || member || ' &folder from v\$logfile order by 1;
select 'host copy ' || name || ' &folder from v\$controlfile order by 1;
select 'host copy ' || name || ' &folder from v\$tempfile order by 1;
spool off

alter table backup controlfile to trace as C:\ora_cold_backup\BckCtl.sql REUSE;

-- Extinction de la base
shutdown immediate;

@&fichier_control;
startup;
exit;


REM Ajouter ici les commandes nécessaire à la sauvegarde des autres fichiers !


REM Dans le cas ou les services ne se sont pas démarré, démarrage préventif
net start OracleJobSchedulerXE
net start OracleServiceXE
net start OracleXEClrAgent
net start OracleXETNSListener

_____________________________________SCRIPT DE SAUVEGARDE MYSQL________________________________________

REM A compléter avec le chemin où se trouve le dit exécutable, par exemple : [path] -> C:\wamp\bin\mysql\mysql5.5.24";
echo '
set chemin="c:\rep_mysql"
set utilisateur=' . Session::get(Constants::$SESSION_ldb) . '
set mdp=' . Session::get(Constants::$SESSION_pdb) . '


md c:\mys_cold_backup
%chemin%\bin\mysqladmin.exe -u %utilisateur% -p %mdp% shutdown
xcopy %chemin%\data c:\mys_cold_backup /E /Y

net start [NOM DU SERVICE MYSQL, ex : wampmysqld]

</pre></p></div>';
        }
        // DEVELOPPEUR
        if ($database->GetRule()->GetName() == 'Developpeur') {
            echo '<h3>Objet</h3>';
            echo '<div>';
            $resultObj = $infos->GetObjects();
            if (!empty($resultObj)) {
//    print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultObj[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineObj = 0;
                $total = 0;
                foreach ($resultObj as $tb) {
                    if ($lineObj % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $key => $value) {
                        if ($key == 'TOTAL')
                            $total += $value;
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineObj++;
                }
                echo '<tr class="total"><td>Total objets</td><td></td><td>' . $total . '</td></tr>';
                echo '</table>';
                echo '<br/><br/>';
            }
            $resultOD = $infos->GetObjectsDetails();
            if (!empty($resultOD)) {
//    print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultOD[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineOD = 0;
                foreach ($resultOD as $tb) {
                    if ($lineOD % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineOD++;
                }
                echo '</table>';
            }
            echo '</div>';

            echo '<h3>Tables</h3>';
            echo '<div>';
            $resultT = $infos->GetTables();
            if (!empty($resultT)) {
//    print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultT[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineT = 0;
                foreach ($resultT as $tb) {
                    if ($lineT % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineT++;
                }
                echo '</table>';
                echo '<br/><br/>';
            }
            $resultTI = $infos->GetTablesIndexes();
            if (!empty($resultTI)) {
//    print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultTI[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineTI = 0;
                foreach ($resultTI as $tb) {
                    if ($lineTI % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineTI++;
                }
                echo '</table>';
            }
            echo '</div>';
            echo '<h3>Vue</h3>';
            echo '<div>';
            $resultV = $infos->GetViews();
            if (!empty($resultV)) {
//    print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultV[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineV = 0;
                foreach ($resultV as $tb) {
                    if ($lineV % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineV++;
                }
                echo '</table>';
            }
            echo '</div>';
            echo '<h3>Méthodes</h3>';
            echo '<div>';
            $resultMt = $infos->GetMethods();
            if (!empty($resultMt)) {
//    print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultMt[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineMt = 0;
                foreach ($resultMt as $tb) {
                    if ($lineMt % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineMt++;
                }
                echo '</table>';
                echo '<br/>- pour chaque méthode l\'affichage de la méthode et la possibilité de sauvegarde des instructions de la méthode.';
            }
            echo '</div>';
            echo '<h3>Déclencheur</h3>';
            echo '<div>';
            $resultDl = $infos->GetTriggers();
            if (!empty($resultDl)) {
//    print_r($result);
                echo '<table class="tableContent" width="100%">';
                echo '<tr>';
                foreach (array_keys($resultDl[0]) as $key) {
                    echo '<th>' . $key . '</th>';
                }
                echo '</tr>';
                $lineDl = 0;
                foreach ($resultDl as $tb) {
                    if ($lineDl % 2)
                        echo '<tr class="style">';
                    else
                        echo '<tr>';
                    foreach ($tb as $value) {
                        echo '<td>' . $value . '</td>';
                    }
                    echo '</tr>';
                    $lineDl++;
                }
                echo '</table>';
            }
            echo '</div>';
            echo '<h3>Script</h3>';
            echo '<div><p><pre>
REM _____________________________________SCRIPT DE SAUVEGARDE ORACLE________________________________________

REM Complétez les différents champs pour que la commande soit fonctionnelle
md c:\ora_hot_backup
expdp system/pwd full=Y directory="c:\ora_hot_backup" dumpfile=full_db.dmb logfile=full_db.log

REM _____________________________________SCRIPT DE SAUVEGARDE MYSQL________________________________________

REM A compléter avec le chemin où se trouve le dit exécutable, par exemple : [path] -> C:\wamp\bin\mysql\mysql5.5.24
set chemin=c:\rep_mysql
set utilisateur=' . Session::get(Constants::$SESSION_ldb) . '
set mdp=' . Session::get(Constants::$SESSION_pdb) . '

md c:\mys_hot_backup
%chemin%\bin\mysqldump.exe -u %utilisateur% -p %mdp% --all-databases>c:\mys_hot_backup\mys_backup.txt
</pre></p></div>';
        }
        echo '</div>';
    }
}
?>

<div class="ui-widget">
    <div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;">
        <p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
            Informations relatives à la base de données séléctionée et aux droits de l'utilisateur.</p>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $("pre").snippet("sql",{style:"bright"});
        $( "#accordion" ).accordion({
            heightStyle: "content"
        });
                
        $("#accordion").accordion({
            change : function(event, ui){
                autoResize();
            }
        });
    });
</script>